/**
 * Name:Simba.js
 * 
 * Author:Simba
 * 
 * CreateAt:2019-4-10
 * 
 */

const sb = (() => {
    const doc = document

    String.prototype.mosaic = function (start, end, code = "*") {
        if (!start && start !== 0) throw new Error("参数有误")
        if (isNaN(start) || start < 0 || start >= this.length)
            throw new Error("参数有误")
        if (!end || end >= this.length) end = this.length
        if (isNaN(end) || end <= start) throw new Error("参数有误")

        var left = this.substring(0, start)
        var middle = code.repeat(end - start)
        var end = this.substring(end)
        return left + middle + end
    }

    String.prototype.toCapitalized = function () {
        return this[0].toUpperCase() + this.substring(1)
    }

    String.prototype.toInt = function () {
        return parseInt(this)
    }

    Date.prototype.getFormatText = function () {
        var now = this
        var yyyy = now.getFullYear()
        var mm = _.repairZero(now.getMonth() + 1)
        var dd = _.repairZero(now.getDate())
        var hh = _.repairZero(now.getHours())
        var mi = _.repairZero(now.getMinutes())
        var ss = _.repairZero(now.getSeconds())
        var str = "日一二三四五六"
        var week = str[now.getDay()]
        return `${yyyy}-${mm}-${dd} ${hh}:${mi}:${ss} 星期${week}`
    }

    Date.prototype.getFormatDate = function () {
        //将字符串通过某字符分割成数组
        return this.getFormatText().split(' ')[0]
    }

    Date.prototype.getFormatTime = function () {
        //将字符串通过某字符分割成数组
        return this.getFormatText().split(' ')[1]
    }

    Date.prototype.getFormatWeek = function () {
        //将字符串通过某字符分割成数组
        return this.getFormatText().split(' ')[2]
    }


    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function (obj) {
            for (let i = 0; i < this.length; i++) {
                if (this[i] === obj) return i
            }
            return -1
        }
    }

    if (!Array.prototype.lastIndexOf) {
        Array.prototype.lastIndexOf = function (obj) {
            for (let i = this.length - 1; i >= 0; i--) {
                if (this[i] === obj) return i
            }
            return -1
        }
    }

    if (!Array.prototype.push) {
        Array.prototype.push = function (obj) {
            this[this.length] = obj
        }
    }

    if (!Array.prototype.join) {
        Array.prototype.join = function (code = "") {
            var str = ""
            for (let i = 0; i < this.length; i++) {
                str += (this[i] + code)
            }
            return str.substring(0, str.length - 1)
        }
    }

    if (!Array.prototype.remove) {
        Array.prototype.remove = function (obj) {
            this.splice(this.indexOf(obj), 1)
        }
    }

    if (!Array.prototype.removeAt) {
        Array.prototype.removeAt = function (index) {
            if (index >= this.length) throw new Error("参数有误")
            this.splice(index, 1)
        }
    }


    if (!Array.prototype.removeAll) {
        Array.prototype.removeAll = function (obj) {
            //找元素在数组中的位置
            var index = this.indexOf(obj)
            //如果找到了
            if (index > -1) {
                //从数组中根据下标删掉
                this.removeAt(index)
                //再次调用自身,直到数组中找不到此元素为止
                arguments.callee.call(this, obj)
            }
        }
    }

    const _ = {
        log() {
            //将数组通过某字符变成字符串
            console.log(Array.prototype.join.call(arguments, ' '))
        },
        repairZero(num) {
            return num < 10 ? ("0" + num) : num
        },
        random(max, min = 0) {
            return Math.round(Math.random() * (max - min)) + min
        },
        makeStr(...args) {
            return args.join('')
        },
        loadScript(url, callback) {
            const head = doc.getElementsByTagName("head")[0]
            var script = doc.createElement("script")
            script.src = this.makeStr(JSDIR, url, "?", this.random())
            script.charset = "utf-8"
            script.async = true
            if (callback) script.onload = callback
            head.appendChild(script)
        },
        isArray(obj) {
            if (Array.isArray) return Array.isArray(obj)
            return this.toString(obj) === '[object Array]'
        },
        isFuction(obj) {
            return this.toString(obj) === '[object Function]'
        },
        toString(obj) {
            return Object.prototype.toString.call(obj)
        }
    }

    return _
})()